package com.cykj.service.serviceImpl;

import com.cykj.Mapper.LoginMapper;
import com.cykj.dto.ResponseDto;
import com.cykj.pojo.User;
import com.cykj.service.LoginService;
import com.cykj.util.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginMapper loginMapper;
    @Override
    public ResponseDto login(String input, String pwd) {
        // 先尝试通过身份证登录
        User loginByID = loginMapper.Login(input, pwd);
        System.out.println(loginByID);
        if (loginByID!= null) {
            HashMap<String, Object> data = new HashMap<>();
            data.put("userInfo",loginByID);
            long Mil = 400000000L;
            String token = JWTUtils.createJWT(data, Mil);
            System.out.println("生成的token"+token);
            HashMap<String, Object> combinData = new HashMap<>();
            combinData.put("loginInfo",loginByID);
            combinData.put("token",token);
            return new ResponseDto(1, "登录成功", combinData);
        }

        // 若身份证登录失败，再尝试通过手机号登录
        User loginByPhone = loginMapper.LoginByPhone(input, pwd);
        if (loginByPhone!= null) {
            HashMap<String, Object> data = new HashMap<>();
            data.put("userInfophone",loginByPhone);
            long Mil = 400000000L;
            String token = JWTUtils.createJWT(data, Mil);
            System.out.println("生成的token"+token);
            HashMap<String, Object> combinData1 = new HashMap<>();
            combinData1.put("loginInfophone",loginByPhone);
            combinData1.put("token",token);
            return new ResponseDto(1, "登录成功", combinData1);
        }

        return new ResponseDto(0, "登录失败", null);
    }

    @Override
    public User LoginUser(int userId) {
        User user = loginMapper.LoginUser(userId);
        return user;
    }
}
